/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.CES.businessLogic;

import com.bionic.CES.DAO.IUserDAO;
import com.bionic.CES.beans.User;
import com.bionic.CES.connections.DAOFactory;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Jarik
 */
public class UserService {

    public static IUserDAO userDAO;

    static {
        DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
        userDAO = factory.getUserDAO();
    }

    public static Integer createNewUser(String name, String surName, String email,
            String password, String adress, String telephone) throws IllegalArgumentException{
        User user = new User();
        if(checkUserLoginAndPassword(email,null)!=null)
        throw new IllegalArgumentException("Such email is already registered");
        user.setName(name);
        user.setSurName(surName);
        user.setEmail(email);
        user.setPassword(password);
        user.setAdress(adress);
        user.setTelephone(telephone);
        try {
            userDAO.add(user);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return checkUserLoginAndPassword(email,password);
    }

    public static void updateCurrentUser(Integer userId, String name, String surName, String email,
            String password, String adress, String telephone) {
        User user = new User();
        user.setUserid(userId);
        user.setName(name);
        user.setSurName(surName);
        user.setEmail(email);
        user.setPassword(password);
        user.setAdress(adress);
        user.setTelephone(telephone);
        try {
            userDAO.update(user);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    public static void deleteCurrentUser(Integer userId){
        try {
            userDAO.deleteById(userId);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    public static User returnCurrentUserInfo(Integer userId){
        User user = new User();
        user.setUserid(userId);
        try {
            List<User> list = userDAO.findByDynamicSelect(user);
            user = ((User) list.toArray()[0]);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return user;
    }

    public static Integer checkUserLoginAndPassword(String email, String password) {
        Integer userId=null;
        User user = new User();
        if (email!=null)
        user.setEmail(email);
        if (password!=null)
        user.setPassword(password);
        try {
            List<User> list = userDAO.findByDynamicSelect(user);
            if (!list.isEmpty()) {
                userId = ((User) list.toArray()[0]).getUserid();
            }          
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return userId;
    }
}
